package org.springblade.modules.api.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.service.ApiService;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springblade.modules.personal.vo.UserBaseVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
@AllArgsConstructor
@RequestMapping("blade-api/external")
@Api(value = "第三方相关接口", tags = "第三方相关接口")
public class ExternalController {

	private final ApiService apiService;
	private final IUserBaseService userBaseService;
	private final IUserService userService;
	/**
	 * 军械厂 组织机构
	 */
	@GetMapping("/jxc/dept")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "组织机构列表")
	public R jxcDept() {
		return R.data(apiService.getUserDept());
	}

	/**
	 * 军械厂 oa人员信息列表
	 */
	@GetMapping("/jxc/oa-user")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "oa人员信息列表")
	public R jxcOaUser(UserBaseVO userBaseVO, Query query) {
		IPage<UserBaseVO> pages = userBaseService.selectUserByDeptsAndIdcard(Condition.getPage(query), userBaseVO);
		return R.data(pages);
	}


	/**
	 * 初始化组织机构
	 * 定时任务
	 */
	@GetMapping("/jxc/user-dept-init")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "初始化组织机构")
	public R jxcUserDeptInit() {
		return R.data(apiService.getApiDept());
	}


	/**
	 * 初始化组织机构
	 * 定时任务
	 */
	@GetMapping("/jxc/oa-user-init")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "初始化oa人员信息列表")
	public R jxcOaUserInit() {
		return R.data(apiService.getApiJxcUser());
	}

	/**
	 * 初始化组织机构
	 * 定时任务
	 */
	@GetMapping("/jxc/oa-user-name-init")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "同步用户表和用户扩展表的姓名")
	public R jxcOaUserNameInit() {
		List<UserBaseEntity> list = userBaseService.list();
		list.forEach(data->{

			User user = userService.getById(data.getUserId());
			if (null==user)return;
			user.setRealName(data.getUserName());
			user.setName(data.getUserName());
			userService.updateById(user);
		});
		return R.success("成功");
	}



	public static void main(String[] args) {
		String input = "这里有一些示例文本，包括#{1234}, #{5678}, 和其他文本#{9101112}。";
		List<String> matches = extractHashContent(input);

		// 将List转换为数组
		System.out.println(matches);



		String input2 = "这里有一些示例文本，包括#{1234}, #{5678}, 和其他文本#{9101112}。";
		Map<String, String> replacements = new HashMap<>();
		replacements.put("1234", "替换1234");
		replacements.put("5678", "替换5678");
		replacements.put("9101112", "替换9101112");

		String result = replaceHashContent(input2, replacements);

		System.out.println(result);
	}

	public static List<String> extractHashContent(String text) {
		// 正则表达式匹配 #{xxxx}，并提取xxxx
		String regex = "#\\{([^}]*)\\}";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(text);

		List<String> matches = new ArrayList<>();
		while (matcher.find()) {
			// group(1) 是第一个括号内匹配的内容，即xxxx
			matches.add(matcher.group(1).trim());
		}
		return matches;
	}

	public static String replaceHashContent(String text, Map<String, String> replacements) {
		Pattern pattern = Pattern.compile("#\\{([^}]+)}");
		Matcher matcher = pattern.matcher(text);
		StringBuffer sb = new StringBuffer();

		while (matcher.find()) {
			String key = matcher.group(1); // 获取xxx的值
			String replacement = replacements.getOrDefault(key, matcher.group(0)); // 根据xxx的值获取替换内容，如果没有找到则不替换
			matcher.appendReplacement(sb, replacement);
		}
		matcher.appendTail(sb);

		return sb.toString();
	}



}
